﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AgentMainFaultList : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentMainFaultList"; }
        }

       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            //
            string deleteScript = Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, gdPar.GetDeleteSelectedReference(), String.Empty);
            // 新增数据初始值
            JObject defaultObj = new JObject();
            defaultObj.Add("Title", "");            
            defaultObj.Add("Delete", String.Format("<a href=\"javascript:;\" onclick=\"{0}\"><img src=\"{1}\"/></a>", deleteScript, IconHelper.GetResolvedIconUrl(Icon.Delete)));
            // 在第一行新增一条数据
            btnAddPar.OnClientClick = gdPar.GetAddNewRecordReference(defaultObj, true);
            // 重置表格
            btnResetPar.OnClientClick = gdPar.GetRejectChangesReference();
            //
            BindGrid1();
            /////222222
            deleteScript = Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, gdSub.GetDeleteSelectedReference(), String.Empty);
            // 新增数据初始值
            JObject defaultObj2 = new JObject();
            defaultObj2.Add("Title2", "");
            defaultObj2.Add("Delete2", String.Format("<a href=\"javascript:;\" onclick=\"{0}\"><img src=\"{1}\"/></a>", deleteScript, IconHelper.GetResolvedIconUrl(Icon.Delete)));
            // 在第一行新增一条数据
            btnAddSub.OnClientClick = gdSub.GetAddNewRecordReference(defaultObj2, true);
            // 重置表格
            btnResetSub.OnClientClick = gdSub.GetRejectChangesReference();
            
        }

        private void BindGrid1()
        {
            this.gdPar.DataSource = AgentMainFaultControlller.GetAllByParId(0);
            this.gdPar.DataBind();
        }

        private void BindGrid2(int parID)
        {
            DataTable dt = AgentMainFaultControlller.GetAllByParId(parID);
            if (dt.Rows.Count <= 0)
            {
                this.gdSub.DataSource =null;
                this.gdSub.DataBind();
                return;
            }
           // return;
            this.gdSub.DataSource = dt;
            this.gdSub.DataBind();
        }

        /// <summary>
        /// 行绑定前
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gdPar_PreDataBound(object sender, EventArgs e)
        {
            //return Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedReference(), String.Empty);

            LinkButtonField deleteField = gdPar.FindColumn("Delete") as LinkButtonField;
            deleteField.OnClientClick = Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, gdPar.GetDeleteSelectedReference(), String.Empty);
        }
        /// <summary>
        /// 保存主要大项的变列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSavePar_Click(object sender, EventArgs e)
        {
            // 删除现有数据
            List<int> deletedRows = gdPar.GetDeletedList();
            foreach (int rowIndex in deletedRows)
            {
                int rowID = Convert.ToInt32(gdPar.DataKeys[rowIndex][0]);
                //DeleteRowByID(rowID);
                AgentMainFaultControlller.Delete(rowID);
            }

            // 修改的现有数据
            Dictionary<int, Dictionary<string, object>> modifiedDict = gdPar.GetModifiedDict();
            foreach (int rowIndex in modifiedDict.Keys)
            {
                if(modifiedDict[rowIndex].ContainsKey("Title"))
                { 
                    int rowID = Convert.ToInt32(gdPar.DataKeys[rowIndex][0]);
                    AgentMainFaultInfo obj = AgentMainFaultControlller.GetById(rowID);
                    obj.Title = modifiedDict[rowIndex]["Title"].ToString();

                    AgentMainFaultControlller.Update(obj);
                }
            }


            // 新增数据
            List<Dictionary<string, object>> newAddedList = gdPar.GetNewAddedList();
            for (int i = 0; i < newAddedList.Count; i++)
            {
                //DataRow rowData = CreateNewData(table, newAddedList[i]);
                //table.Rows.Add(rowData);
                AgentMainFaultInfo obj = new AgentMainFaultInfo();
                obj.ParId = 0;
                obj.Enabled = true;
                obj.Title = newAddedList[i]["Title"].ToString();
                AgentMainFaultControlller.Add(obj);
            }

            BindGrid1();

            Alert.Show("数据保存成功");
        }

        protected void gdPar_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            int parid = Convert.ToInt32(gdPar.DataKeys[e.RowIndex][0]);
            BindGrid2(parid);
        }
        protected void gdPar_RowClick(object sender, GridRowClickEventArgs e)
        {
            
        }

        protected void gdSub_PreDataBound(object sender, EventArgs e)
        {
            LinkButtonField deleteField = gdSub.FindColumn("Delete2") as LinkButtonField;
            deleteField.OnClientClick = Confirm.GetShowReference("删除选中行？", String.Empty, MessageBoxIcon.Question, gdSub.GetDeleteSelectedReference(), String.Empty);
        }

        protected void btnSaveSub_Click(object sender, EventArgs e)
        {
            int ParId = 0;
            try
            {
                 ParId = Convert.ToInt32(gdPar.DataKeys[gdPar.SelectedRowIndex][0]);
            }
            catch {
                Alert.ShowInTop("请先选择一个故障大类");
                return;
            }

            // 删除现有数据
            List<int> deletedRows = gdSub.GetDeletedList();
            foreach (int rowIndex in deletedRows)
            {
                int rowID = Convert.ToInt32(gdSub.DataKeys[rowIndex][0]);
                //DeleteRowByID(rowID);
                AgentMainFaultControlller.Delete(rowID);
            }

            // 修改的现有数据
            Dictionary<int, Dictionary<string, object>> modifiedDict = gdSub.GetModifiedDict();
            foreach (int rowIndex in modifiedDict.Keys)
            {
                if (modifiedDict[rowIndex].ContainsKey("Title2"))
                {
                    int rowID = Convert.ToInt32(gdSub.DataKeys[rowIndex][0]);
                    AgentMainFaultInfo obj = AgentMainFaultControlller.GetById(rowID);
                    obj.Title = modifiedDict[rowIndex]["Title2"].ToString();

                    AgentMainFaultControlller.Update(obj);
                }
            }


            // 新增数据
            List<Dictionary<string, object>> newAddedList = gdSub.GetNewAddedList();
            for (int i = 0; i < newAddedList.Count; i++)
            {
                //DataRow rowData = CreateNewData(table, newAddedList[i]);
                //table.Rows.Add(rowData);
                AgentMainFaultInfo obj = new AgentMainFaultInfo();
                obj.ParId = ParId;
                obj.Enabled = true;
                obj.Title = newAddedList[i]["Title2"].ToString();
                AgentMainFaultControlller.Add(obj);
            }

            BindGrid2(ParId);

            Alert.Show("数据保存成功");
        }

        protected void gdPar_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "ViewSub")
            {
                gdPar.SelectedRowIndex= e.RowIndex;
                int ParId = Convert.ToInt32(gdPar.DataKeys[gdPar.SelectedRowIndex][0]);
                BindGrid2(ParId);
            }
        }

        
    }
}